package cn.wolfcode.luowowo.job;

import cn.wolfcode.louwowo.cache.key.RedisKeys;
import cn.wolfcode.louwowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.louwowo.cache.service.IWendaStatisRedisService;
import cn.wolfcode.louwowo.cache.vo.HuidaStatisVO;
import cn.wolfcode.louwowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.article.service.IHuidaService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.DetailVO;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *  redis数据的持久化
 *  注解scheduled是定时任务标签
 *  cron表达式是任务计划，可以上网找在线生成器
 *  Spring支持的是没有年的cron表达式
 *
 */
@Component
public class RedisDataPersistenceJob {
    @Reference
    private IStrategyStatisRedisService redisService;
    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private IWendaStatisRedisService wendaStatisRedisService;
    @Reference
    private IHuidaService huidaService;

    /*@Scheduled(cron = "0 0 0 * * ? ")
    public void doPersistenceJob(){
        //获取redis所有的vo数据
        List<StrategyStatisVO> voList = redisService.getVosByPattern(RedisKeys.STRATEGY_STATIS_VO.join("*"));
        for (StrategyStatisVO vo : voList) {
            DetailVO detailVO = new DetailVO();
            BeanUtil.copy(detailVO, vo);
            detailService.updateStatisByVo(detailVO);
        }
    }*/

    /**
     * 持久化回答的数据
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    public void doWendaJob(){
        System.out.println("------------------------begin-----------------------------------");
        List<HuidaStatisVO> voList = wendaStatisRedisService.getVosByPattern(RedisKeys.HUIDA_STATIS_VO.join("*"));
        for (HuidaStatisVO vo : voList) {
            DetailVO detailVO = new DetailVO();
            BeanUtil.copy(detailVO, vo);
            detailVO.setHuidaId(vo.getHuidaId());
            huidaService.updateStatisByVo(detailVO);
        }
        System.out.println("-------------------------end----------------------------------------");
    }
}
